#include "TTip.h"
#include "TScale9.h"
#include "../utils/Utils.h"
#include "TUI.h"


/***************************************************
 * TTip
 **************************************************/
TTip::TTip(){ 
    m_pOperPanel=NULL;
}
bool TTip::init(){
     
    if(!TPanel::init()){
        return false;
    } 
    
    return true;
}


void TTip::setOperPanel(TTipOper* panel){
    TPanel* container= (TPanel* ) getUI("operPanel");
    container->removeAllChildrenWithCleanup(true);
    if(panel==NULL){
        return;
    }
    panel->setTip(this);
    panel->setTag(1);//为了引导
    this->m_pOperPanel=panel;
    container->addChild(panel);
    
    if(!m_pOperPanel->hasLoadConf){
        m_pOperPanel->loadConf(m_pOperPanel->confPath);
    }
}


void TTip::clear(){ 
}


 
 
void TTip::registerWithTouchDispatcher()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
}

  

void TTip::procTuiEvent(const string& event,TPanelItem*){
    if(event=="closeTip"){
        hide();
    }
}
void TTip::hide(){
    if(this->getParent()){
        this->getParent()->removeChild(this, true);
    }
}
 

bool TTip::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
    
    if(!m_bTouchEnable){
        return false;
    }
    
    CCPoint clickPos= pTouch->getLocation();
    
    CCPoint pos= getPosInTarget(clickPos, this);
    
    if(pos.x<0 || pos.x>getContentSize().width
       ||pos.y>0 || pos.y<-getContentSize().height  ){
         
        hide();
        return false;
    } 
    TPanel::ccTouchBegan(pTouch, pEvent);
  
    
    return true;
}



TLabel* TTip::addText( TPanelItem* container, string & label,const TextFormat& format,bool breakLine){
    if(breakLine){
        
        replaceStr(label," ", "\n");
    }
    TLabel* txt=TLabel::create(format,label.c_str());  
    container->addChild(txt);
    txt->setPositionY(curInfoHeight);
    
    curInfoHeight-=txt->getContentSize().height+10;
    
    return txt;
    
    
}
 

